package com.rdc.example.controller;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.constant.ResultConst;
import com.rdc.example.service.impl.UserServiceImpl;
import com.rdc.example.view.UserView;
import com.rdc.example.view.UtilView;

import java.util.List;

import static com.rdc.example.controller.MainController.userController;


/**
 * @author duck
 * @description 用户controller控制层
 */
public class UserController {

    private static UserServiceImpl userService = new UserServiceImpl();
    private static UserView userView=new UserView();
    public static UtilView utilView=new UtilView();
    public static UserDTO userDTO = new UserDTO();


    public static void main(String[] args) throws Exception {
        UserController userController = new UserController();

        System.out.println(userService.getUserByUserName("路人甲"));

    }

     static void showLoginMenu() throws Exception {
        utilView.showMessage("=== 登录 ===");
        String userName= UtilView.getUserInputString("用户名：");
        String password= UtilView.getUserInputString("密码：");
        Result result= userController.login(userName, password);
        if(result.getCode()==200){
            MainController.isLogIn=true;
            MainController.loggedInuserDTO=userController.getUserByName(userName);

        }
        utilView.showMessage(result.getMessage());

    }

    static void showRegisterMenu() throws Exception {
        UserServiceImpl userService = new UserServiceImpl();
        UserDTO userDTO = new UserDTO();
        userDTO.setUserName(UtilView.getUserInputString("用户名："));
        userDTO.setPassword(UtilView.getUserInputString("密码(6-15位)： "));
        userDTO.setEmail(UtilView.getUserInputString("邮箱(选填)： "));
        Result result= userController.register(userDTO);
        if(result.getCode()==200){
            MainController.isLogIn=true;
            MainController.loggedInuserDTO=userController.getUserByName(userDTO.getUserName());
        }
        utilView.showMessage(result.getMessage());
    }

    /**
     * 用户登录
     * @param userName 用户名
     * @param password 密码
     * @return 返回登录结果Result
     */
    public Result login(String userName, String password) throws Exception {
        // 检查用户名是否为空
        if (userName == null || userName.trim().isEmpty()) {
            return Result.fail("用户名不可为空", ResultConst.FAIL);
            //return Result(ResultConst.FAIL,"用户名不可为空")
        }
        // 检查密码长度是否在 6 到 15 位之间
        if (password == null || password.length() < 6 || password.length() > 15) {
            return Result.fail("密码长度需为6-15位！", ResultConst.FAIL);
        }
        try{
            return userService.login(userName, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 用户注册
     * @param userDTO
     * @return 返回登录结果Result
     */
    public Result register(UserDTO userDTO){
        if (userDTO.getUserName() == null || userDTO.getUserName().trim().isEmpty()) {
            return Result.fail("用户名不可为空", ResultConst.FAIL);
            //return Result(ResultConst.FAIL,"用户名不可为空")
        }
        // 检查密码长度是否在 6 到 15 位之间
        if (userDTO.getPassword() == null || userDTO.getPassword().length() < 6 || userDTO.getPassword().length() > 15) {
            return Result.fail("密码长度需为6-15位！", ResultConst.FAIL);
        }
        try{
            return userService.register(userDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过名字获取用户类
     * @param userName
     * @return userDTO
     */
    public UserDTO getUserByName(String userName) throws Exception {
        try{
            userDTO= userService.getUserByUserName(userName);
            return userDTO;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    /**
     * 展示用户基本信息
     * @param userDTO
     */
    public static void showUserMessage(UserDTO userDTO){
        userView.showUserMessage(userDTO);
    }


    public static void searchUser()  {
        try{
            String key=userView.getInputString();
            if(key==null){
                utilView.showMessage("输入不可为空！");
                return;
            }
            List<UserDTO> list =userService.searchUsersByName(key);
            if(list.isEmpty()){
                utilView.showMessage("搜索结果为空");
                return;
            }
            for(int i=0;i<list.size();i++) {
                userView.showUserDTOList(i,list.get(i));
            }
            MainController.currentUserDTOList=list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean turnToUser(List<UserDTO> userDTOList){
        try{
        if (userDTOList.isEmpty()){
            utilView.showMessage("没有用户可操作，空空如也");
            return false;
        }
        Integer index = utilView.getUserInputInteger("请输入进行操作的用户号码：");
        if (index > userDTOList.size() || index < 1) {
            utilView.showMessage("没有对应的用户");
            return false;
        }
            MainController.currentUserDTO = userDTOList.get(index - 1);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }




}
